package com.funny.learning.english.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.funny.learning.english.GameBaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.model.LangNgheVaVietEntity;
import com.funny.learning.english.util.Utils;

public class LangNgheVaVietActivity1 extends GameBaseActivity implements
		OnClickListener {

	private LinearLayout llListWord;
	private LayoutInflater inflater;

	private ArrayList<String> arrDataWord;

	private int countTrue = 0;

	private int countScreen = 0;

	private Animation animation6;

	private TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10;
	private Animation animation1;
	private Animation animation2;
	private Animation animation3;
	private Animation animation4;
	private Animation animation5;
	private Animation animation9;
	private Animation animation7;
	private Animation animation8;
	private Animation animation10;
	private ImageView ivDes;
	private int srcSound;
	private ImageView ivSound;
	private ImageView ivHelp;
	
	private ArrayList<LangNgheVaVietEntity> arrLangNgheVaViet = new ArrayList<LangNgheVaVietEntity>();

	private String TAG = "LangNgheVaVietActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lang_nghe_viet_layout);
		Log.d(TAG, "Oncreate");
		inflater = getLayoutInflater();
		
		initUI();

	}

	 private void RefreshDataResult(ArrayList<LangNgheVaVietEntity> arrDataWordDapan){
		 if(llListWord != null){
			 llListWord.removeAllViews();
		 }
		 int lengthArr = arrDataWordDapan.size();
		 if(lengthArr > 0){
			 for (int i = 0; i < arrDataWordDapan.size(); i++) {
				 
				 View view = inflater.inflate(R.layout.langngh_viet_item, null);
				
				 TextView tvWord = (TextView)view.findViewById(R.id.tvWord);
				 LinearLayout llIndex = (LinearLayout)view.findViewById(R.id.llIndex);
				 tvWord.setText(arrDataWordDapan.get(i).getWord());
				
				 if(arrDataWordDapan.get(i).isVisible()){
					 llIndex.setBackgroundResource(android.R.color.transparent);
					 tvWord.setVisibility(View.VISIBLE);
				 }else{
					 llIndex.setBackgroundResource(R.drawable.bg_text_invi1);
					 tvWord.setVisibility(View.INVISIBLE);
				 }
				 llListWord.addView(view);
			 }
		 }
		 llListWord.invalidate();
	 }

	private void loadDataGrid(ArrayList<String> arrDataWord) {
		tv1.setText(arrDataWord.get(0));
		tv2.setText(arrDataWord.get(1));
		tv3.setText(arrDataWord.get(2));
		tv4.setText(arrDataWord.get(3));
		tv5.setText(arrDataWord.get(4));
		tv6.setText(arrDataWord.get(5));
		tv7.setText(arrDataWord.get(6));
		tv8.setText(arrDataWord.get(7));
		tv9.setText(arrDataWord.get(8));
		tv10.setText(arrDataWord.get(9));

	}

//	private void setAnimation(final Animation animation, final TextView view) {
//		view.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(final View arg01) {
//				for (int i = 0; i < arrLangNgheVaViet.size(); i++) {
//					LangNgheVaVietEntity dataDapAnEntity = arrLangNgheVaViet.get(i);
//
//					String textClick = view.getText().toString().trim();
//
//					if (dataDapAnEntity.getWord().trim().contains(textClick)) {
//						if (arrLangNgheVaViet.get(countTrue).getWord()
//								.equals(textClick)) {
//							view.setBackgroundResource(R.drawable.chon_tu_dung);
//							arrLangNgheVaViet.get(countTrue).setVisible(true);
//							RefreshDataResult(arrLangNgheVaViet);
//							countTrue = countTrue + 1;
//							if (countTrue >= arrLangNgheVaViet.size()) {
//								countTrue = 0;
//								countScreen = countScreen + 1;
//								if (countScreen >= mGameEntities.size()) {
//									onBackPressed();
//									Toast.makeText(
//											activity,
//											"Ban da hoan thanh nhom cau hoi nay!!!!",
//											1000).show();
//								} else {
//									initData(countScreen);
//								}
//							}
//						} else {
//							view.setBackgroundResource(R.drawable.chon_tu_sai);
//						}
//
//						break;
//					} else {
//						view.setBackgroundResource(R.drawable.chon_tu_sai);
//					}
//				}
//
//				view.startAnimation(animation);
//
//				animation.setAnimationListener(new AnimationListener() {
//					@Override
//					public void onAnimationStart(Animation arg0) {
//					}
//
//					@Override
//					public void onAnimationRepeat(Animation arg0) {
//					}
//
//					@Override
//					public void onAnimationEnd(Animation arg0) {
//						view.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
//					}
//				});
//
//			}
//		});
//	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivSound:
			// playMusic(srcSound);
			break;
		case R.id.ivHelp:
			// if(countTrue < arrDataWordDapan.size()){
			// arrDataWordDapan.get(countTrue).setInvisible(true);
			// RefreshData(arrDataWordDapan);
			// countTrue = countTrue + 1;
			// if(countTrue >= arrDataWordDapan.size()){
			// countTrue = 0;
			// countScreen = countScreen + 1;
			// if(countScreen >= arrDataNgheViet.size()){
			// Toast.makeText(activity, "Ban da hoan thanh :" + countScreen
			// +" cau hoi", 1000).show();
			// }else{
			// fillData(countScreen);
			// }
			// }
			// }
			break;
		default:
			break;
		}
	}

	// private void fillData(int count){
	// arrDataWordDapan = arrDataNgheViet.get(count).getArrDapan();
	// arrDataWord = arrDataNgheViet.get(count).getArrWordList();
	//
	// ivDes.setImageResource(arrDataNgheViet.get(count).getSrcImg());
	// srcSound = arrDataNgheViet.get(count).getSrcSound();
	//
	// // playMusic(srcSound);
	//
	// RefreshData(arrDataWordDapan);
	// loadDataGrid(arrDataWord);
	// }

	private void initUI() {
		ivDes = (ImageView) findViewById(R.id.ivDes);

		ivSound = (ImageView) findViewById(R.id.ivSound);
		ivHelp = (ImageView) findViewById(R.id.ivHelp);

		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv3);
		tv4 = (TextView) findViewById(R.id.tv4);
		tv5 = (TextView) findViewById(R.id.tv5);
		tv6 = (TextView) findViewById(R.id.tv6);
		tv7 = (TextView) findViewById(R.id.tv7);
		tv8 = (TextView) findViewById(R.id.tv8);
		tv9 = (TextView) findViewById(R.id.tv9);
		tv10 = (TextView) findViewById(R.id.tv10);

		ivSound.setOnClickListener(this);
		ivHelp.setOnClickListener(this);

		tv1.setOnClickListener(this);
		tv2.setOnClickListener(this);
		tv3.setOnClickListener(this);
		tv4.setOnClickListener(this);
		tv5.setOnClickListener(this);
		tv6.setOnClickListener(this);
		tv7.setOnClickListener(this);
		tv8.setOnClickListener(this);
		tv9.setOnClickListener(this);
		tv10.setOnClickListener(this);

		llListWord = (LinearLayout) findViewById(R.id.llListWord);

		animation1 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation2 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation3 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation4 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation5 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation6 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation9 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation7 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation8 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation10 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		
//		setAnimation(animation1, tv1);
//		setAnimation(animation2, tv2);
//		setAnimation(animation3, tv3);
//		setAnimation(animation4, tv4);
//		setAnimation(animation5, tv5);
//		setAnimation(animation6, tv6);
//		setAnimation(animation7, tv7);
//		setAnimation(animation8, tv8);
//		setAnimation(animation9, tv9);
//		setAnimation(animation10, tv10);
	}

	@Override
	protected void initSucess() {
		super.initSucess();
		Log.d(TAG, "initSucess");
		initUI();
//		Toast.makeText(activity, "size: " + mGameEntities.size(), 2000).show();
		
//		loadWordResult(mGameEntities.get(countTrue).getSearchEN());
		
		initData(countScreen);
	}
	
	private void initData(int count){
		Log.d(TAG, "getSearchEN::" + mGameEntities.get(count).getLname());
		// Load data hien thi O ket qua va du lieu chon
		loadWordData(mGameEntities.get(count).getLname());

		// Hien thi du lieu du lieu Ket qua
		RefreshDataResult(arrLangNgheVaViet);
	}

	private void loadWordData(String strResult){
		arrDataWord = new ArrayList<String>();
		arrLangNgheVaViet = new ArrayList<LangNgheVaVietEntity>();
		char[] cResult = strResult.toCharArray();
		int sizeWord = cResult.length;
		
		Log.d(TAG, "sizeWord::" + sizeWord);
		
		for (int i = 0; i < sizeWord; i++) {
			String strWord = String.valueOf(cResult[i]);
			
			Log.d(TAG, "sizeWord::" + strWord);
			
			arrDataWord.add(strWord);
			arrLangNgheVaViet.add(new LangNgheVaVietEntity(strWord, false));
		}
		switch (sizeWord) {
		case 2:
			addMoreWordData(2);
			break;
		case 3:
			addMoreWordData(3);
			break;
		case 4:
			addMoreWordData(4);
			break;
		case 5:
			addMoreWordData(5);
			break;
		case 6:
			addMoreWordData(6);
			break;
		case 7:
			addMoreWordData(7);
			break;
		case 8:
			addMoreWordData(8);
			break;
		case 9:
			addMoreWordData(9);
			break;

		default:
			break;
		}
	}
	
	private void addMoreWordData(int size){
		for (int i = 0; i < (10-size); i++) {
			
			String str = Utils.randomString();
			Log.d(TAG, "addMoreWordData::" + str);
			arrDataWord.add(str);
		}
		Log.d(TAG, "addMoreWordData Size::" + arrDataWord.size());
		// Hien thi du lieu hien co de chon
		loadDataGrid(arrDataWord);
	}
	
	protected void intAB(String title) {
		ImageView ivBack = (ImageView) findViewById(R.id.ivBack);
		TextView tvTitleAB = (TextView) findViewById(R.id.tvTitleAB);

		tvTitleAB.setText(title);

		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onBackPressed();
			}
		});
	}
}
